package com.proxy.thread;

import com.sun.deploy.util.StringUtils;

import java.util.concurrent.*;

/**
 * DesignPatterns
 *
 * @Title: com.proxy.thread
 * @Date: 2021/1/11 0011 23:08
 * @Author: wfg
 * @Description:
 * @Version:
 */
public class TestThreadLocal  {

    private static ThreadLocal<String> local = new ThreadLocal<>();
    public static void main(String[] args) {
        int i1 = Runtime.getRuntime().availableProcessors();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(i1);
        for (int i = 0; i < 8; i++) {
            int finalI = i;
            fixedThreadPool.execute(new Runnable() {
                public void run() {
                    try {

                        String s = local.get();

                        if(s==null || "".equals(s)){
                            local.set(finalI+"");
                        }
                        String s2 = local.get();
                        System.out.println("线程"+Thread.currentThread().getName()+ "值1:  " + s +"值2:   " + s2);

                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

}
